package com.sanji.sjmallutils;

import com.sanji.sjmallmodule.goods.GoodsBean;
import com.sanji.sjmallmodule.goods.SearchTypeBean;
import com.sanji.sjmallmodule.order.ShopcarChildBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fhm on 2017/2/28.
 */
public class JsonParseUtil {
    /**
     * @return 解析热销产品
     */
    public static List<GoodsBean> hotGoodsParse(JSONArray array) {
        List<GoodsBean> list = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            GoodsBean goodsBean = new GoodsBean();
            JSONObject object = (JSONObject) array.opt(i);
            Object obj = object.opt("price");
            String id = object.optString("id");
            String goodsName = object.optString("goodsName");
            String picSrc = object.optString("picSrc");
            goodsBean.setPhoneName(goodsName);
            goodsBean.setUrl(picSrc);
            goodsBean.setTopFlag(i);
            goodsBean.setSkuId(id);
            goodsBean.setPhonePrice(Double.valueOf(obj.toString()));
            goodsBean.setSkuNum(object.optString("skuNum"));
            goodsBean.setGoodsCode(object.optString("goodsNum"));//商品编码
            list.add(goodsBean);
        }
        return list;
    }

    /**
     * @return 解析商品品牌列表
     */
    public static List<SearchTypeBean> goodsBrandParse(JSONArray array, int lineType) {
        List<SearchTypeBean> list = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.optJSONObject(i);
            String name = object.optString("NAME");
            String id = object.optString("ID");
            SearchTypeBean searchTypeBean = new SearchTypeBean(name, id, lineType);
            list.add(searchTypeBean);
        }
        return list;
    }


    /**
     * 购物车解析
     * @param goods
     * @return
     */
    public static ShopcarChildBean shopcarDetailParse(JSONObject goods){
        ShopcarChildBean childBean = new ShopcarChildBean();
        childBean.setGroupTitle(goods.optString("type"));
        childBean.setGoodsName(goods.optString("goodsName"));
        childBean.setGoodsPrice(goods.optDouble("singlePrice"));
        childBean.setImgUrl(goods.optString("default_img"));
        childBean.setGoodsSpec(goods.optString("edition"));
        childBean.setColor(goods.optString("color"));
        childBean.setGoodsCount(goods.optInt("quantity"));
        childBean.setInventoryCount(goods.optInt("stock"));
        childBean.setLimitCount(goods.optInt("stock"));
        childBean.setGoodsId(goods.optString("goodsId"));
        childBean.setCartId(goods.optString("cartId"));
        childBean.setSubTotal(goods.optDouble("subTotalPrice"));
        childBean.setSkuId(goods.optString("skuId"));
        childBean.setGoodsNum(goods.optString("goodsNum"));
        //赠品
        JSONArray gifts = goods.optJSONArray("gifts");
        if (gifts!=null && gifts.length() > 0){
            List<String> giftList = new ArrayList<>();
            for (int m = 0;m<gifts.length();m++){
                JSONObject gift = gifts.optJSONObject(m);
                String giftStr = gift.optString("goodsName");
                giftList.add(giftStr);
            }
            childBean.setGiftList(giftList);
        }
        return childBean;
    }
}
